using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

namespace SgsUI
{
    [RequireComponent(typeof(RectTransform))]
    [RequireComponent(typeof(LabelRes))]

    [Serializable]
    public class ColorData
    {
        public int r = 0;
        public int g = 0;
        public int b = 0;
        public int a = 0;
    }

    public class ViewLabel : ViewNode
    {
        [SerializeField] private string text;

        [SerializeField] private ColorData color;

        [SerializeField] private int fontsize;

        LabelRes m_label;

        protected ViewLabel()
        {
            type = "label";
        }

        public override void UpdateParam()
        {
            base.UpdateParam();
            UpdateLabel();
        }

        public void UpdateLabel()
        {
            if(m_label == null)
                m_label = GetComponent<LabelRes>();

            if(m_label == null)
                return;

            text = m_label.text;
            color = new ColorData()
            {
                r = (int)(m_label.color.r*255),
                g = (int)(m_label.color.g*255),
                b = (int)(m_label.color.b*255),
                a = (int)(m_label.color.a*255)
            };
            fontsize = m_label.fontSize;
        }

        public override void InitViewNode()
        {
            if(TryGetComponent<RectTransform>(out RectTransform var_rect))
            {
                var_rect.anchorMax = Vector2.zero;
                var_rect.anchorMin = Vector2.zero;
                var_rect.sizeDelta = Vector2.zero;
                var_rect.pivot = new Vector2(0.5f,0.5f);
            }

            if(TryGetComponent<LabelRes>(out LabelRes label))
            {
                label.InitRes();
            }
        }

        protected override string GetDefaultName()
        {
            return "label";
        }

        public override bool HasAtriSize()
        {
            return false;
        }

        protected override void WriteOtherParam(NodeData nodeData)
        {
            if(text != String.Empty)
            {
                ParamData param_text = new ParamData();
                param_text.SetValue("text",LuaHelper.Instance.GetTransTextKey(text));//text);
                nodeData.AddParam(param_text);
            }

            ParamData param_size = new ParamData();
            param_size.SetValue("size",fontsize);
            nodeData.AddParam(param_size);

            ParamData param_color = new ParamData();
            param_color.SetValue("color",color);
            nodeData.AddParam(param_color);
        }
    }
}